//
//  YSVipServiceItemCell.m
//  Yueshi
//
//  Created by Northwan on 16/4/13.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSVipServiceItemCell.h"


#define SIZE_GAP_IMG 10
#define SIZE_IMAGE 90
#define SIZE_GAP_LEFT 10



@interface YSVipServiceItemCell()
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UIScrollView *containerScrollView;

@end

@implementation YSVipServiceItemCell

-(void)setModel:(YSVipServiceItemModel *)model{
    _model = model;
    [self.topImageView setImageURLStr:model.topImage];
    [self.containerScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSInteger prodCount = [[model productList] count];
    NSInteger reviewsCount = [[model reviews] count];
    if (prodCount){
        for (NSInteger i=0; i<prodCount; i++) {
            UIImageView *thumbView = [[UIImageView alloc]init];
            thumbView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didThumViewWithProduct:)];
            [thumbView addGestureRecognizer:tap];
            thumbView.tag = i;
            thumbView.contentMode = UIViewContentModeScaleAspectFill;
            thumbView.backgroundColor = [UIColor lightGrayColor];
            thumbView.clipsToBounds = YES;
            if (i < prodCount) {
                thumbView.frame = CGRectMake(SIZE_GAP_LEFT+(SIZE_GAP_IMG+SIZE_IMAGE)*i, 10, SIZE_IMAGE, SIZE_IMAGE);
                thumbView.hidden = NO;
                YSProductList *product = [model productList][i];
                [thumbView setImageURLStr:product.prodThumbnails];
            } else {
                thumbView.hidden = YES;
            }
            [self.containerScrollView addSubview:thumbView];
        }
        float cw = SIZE_GAP_LEFT*2+(SIZE_GAP_IMG+SIZE_IMAGE)*prodCount;
        if (cw<self.width) {
            cw = self.width;
        }
        if (self.containerScrollView.contentSize.width!=cw) {
            self.containerScrollView.contentSize = CGSizeMake(cw, 0);
        }
    }else{
        for (NSInteger i=0; i<reviewsCount; i++) {
            UIImageView *thumbView = [[UIImageView alloc]init];
            thumbView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didThumViewWithReview:)];
            [thumbView addGestureRecognizer:tap];
            thumbView.tag = i;
            thumbView.contentMode = UIViewContentModeScaleAspectFill;
            thumbView.backgroundColor = [UIColor lightGrayColor];
            thumbView.clipsToBounds = YES;
            if (i < reviewsCount) {
                thumbView.frame = CGRectMake(SIZE_GAP_LEFT+(SIZE_GAP_IMG+SIZE_IMAGE)*i, 10, SIZE_IMAGE, SIZE_IMAGE);
                thumbView.hidden = NO;
                YSReviews *reviews = [model reviews][i];
                [thumbView setImageURLStr:reviews.imgList];
            } else {
                thumbView.hidden = YES;
            }
            [self.containerScrollView addSubview:thumbView];
        }
        
        float cw = SIZE_GAP_LEFT*2+(SIZE_GAP_IMG+SIZE_IMAGE)*reviewsCount;
        if (cw<self.width) {
            cw = self.width;
        }
        if (self.containerScrollView.contentSize.width!=cw) {
            self.containerScrollView.contentSize = CGSizeMake(cw, 0);
        }
    }
}

- (void) didThumViewWithReview:(UITapGestureRecognizer *)sender{
    if ([_delegate respondsToSelector:@selector(didSelectThumbViewWithReview:)]){
        [_delegate didSelectThumbViewWithReview:self.model.reviews[sender.view.tag]];
    }
}

- (void) didThumViewWithProduct:(UITapGestureRecognizer *)sender{
    if ([_delegate respondsToSelector:@selector(didSelectThumbViewWithProduct:)]){
        [_delegate didSelectThumbViewWithProduct:self.model.productList[sender.view.tag]];
    }
}

@end
